﻿using RimWorld;
using RimWorld.Planet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SZ.Mech;
using UnityEngine;
using Verse;
namespace SZ.Mech
{
    [StaticConstructorOnStartup]
    public class Verb_DragonFlyMap : Verb
    {
        public static readonly Texture2D TargeterMouseAttachment = ContentFinder<Texture2D>.Get("UI/Abilities/SZ_FlyUI");
        public override Texture2D UIIcon => base.UIIcon;

        DragonFlyExtension dragonFlyExtension;

        protected override bool TryCastShot()
        {
            Find.Targeter.BeginTargeting(TargetingParameters.ForDropPodsDestination(), delegate (LocalTargetInfo x)
            {

                dragonFlyExtension = EquipmentSource.def.GetModExtension<DragonFlyExtension>();
                var tile = caster.Map.Tile;
                var map = Find.WorldObjects.WorldObjectAt<MapParent>(tile);
                TryLaunch(tile, new TransportPodsArrivalAction_VerbLandInSpecificCell(map, x.Cell, dragonFlyExtension.activeDropPodFly, dragonFlyExtension.dropPodIncomingFly));
                ///重装填设定
                ReloadableCompSource.UsedOnce();

            }, null, null, TargeterMouseAttachment);
            return true;
        }

        public void TryLaunch(int destinationTile, TransportPodsArrivalAction arrivalAction)
        {
            if (!this.caster.Spawned)
            {
                Log.Error(string.Concat("Tried to launch ", this.caster, ", but it's unspawned."));
                return;
            }
            Map map = caster.Map;
            //跨图飞行的单位必须是ActiveDropPodInfo


            ActiveDropPod activeDropPod = (ActiveDropPod)ThingMaker.MakeThing(dragonFlyExtension.activeDropPodFly);//行动吊舱                                                                                                 //ActiveDropPod activeDropPod = (ActiveDropPod)ThingMaker.MakeThing(SZ_DefOf.SZ_DragonFlyA);//行动吊舱
            activeDropPod.Contents = new ActiveDropPodInfo();
            caster.DeSpawn(DestroyMode.Vanish);
            activeDropPod.Contents.innerContainer.TryAddOrTransfer(caster, true);

            //飞行离开
            FlyShipLeavingPawnDraw obj = (FlyShipLeavingPawnDraw)SkyfallerMaker.MakeSkyfaller(dragonFlyExtension.dropPodLeavingFly, activeDropPod);//飞行中的效果                                                                                                         //FlyShipLeaving obj = (FlyShipLeaving)SkyfallerMaker.MakeSkyfaller(SZ_DefOf.DropPodLeavingDragonFlyA, activeDropPod);//飞行中的效果

            //obj.innerContainer.TryAddOrTransfer(caster);

            obj.groupID = Find.UniqueIDsManager.GetNextTransporterGroupID();//可能可以这样写
            obj.destinationTile = destinationTile;
            obj.arrivalAction = arrivalAction;//到达行动
            obj.worldObjectDef = WorldObjectDefOf.TravelingTransportPods;
            //compTransporter.CleanUpLoadingVars(map);
            //compTransporter.parent.Destroy();
            GenSpawn.Spawn(obj, caster.Position, map);
        }

        //public void TryLaunch(int destinationTile, TransportPodsArrivalAction arrivalAction)
        //{
        //    if (!this.caster.Spawned)
        //    {
        //        Log.Error(string.Concat("Tried to launch ", this.caster, ", but it's unspawned."));
        //        return;
        //    }
        //    Map map = caster.Map;
        //    //int num = Find.WorldGrid.TraversalDistanceBetween(map.Tile, destinationTile);
        //    ActiveDropPod activeDropPod = (ActiveDropPod)ThingMaker.MakeThing(dragonFlyExtension.activeDropPodFly);//行动吊舱
        //                                                                                                           //ActiveDropPod activeDropPod = (ActiveDropPod)ThingMaker.MakeThing(SZ_DefOf.SZ_DragonFlyA);//行动吊舱
        //    activeDropPod.Contents = new ActiveDropPodInfo();
        //    caster.DeSpawn(DestroyMode.Vanish);

        //    activeDropPod.Contents.innerContainer.TryAddOrTransfer(caster, true);

        //    //activeDropPod.Contents.innerContainer.TryAdd(caster,true);
        //    //activeDropPod.Contents.innerContainer.TryAddRangeOrTransfer(new List<Thing> { caster });

        //    //飞行离开
        //    FlyShipLeaving obj = (FlyShipLeaving)SkyfallerMaker.MakeSkyfaller(dragonFlyExtension.dropPodLeavingFly, activeDropPod);//飞行中的效果                                                                                                         //FlyShipLeaving obj = (FlyShipLeaving)SkyfallerMaker.MakeSkyfaller(SZ_DefOf.DropPodLeavingDragonFlyA, activeDropPod);//飞行中的效果

        //    obj.groupID = Find.UniqueIDsManager.GetNextTransporterGroupID();//可能可以这样写
        //    obj.destinationTile = destinationTile;
        //    obj.arrivalAction = arrivalAction;//到达行动
        //    obj.worldObjectDef = WorldObjectDefOf.TravelingTransportPods;
        //    //compTransporter.CleanUpLoadingVars(map);
        //    //compTransporter.parent.Destroy();
        //    GenSpawn.Spawn(obj, caster.Position, map);
        //}
    }
}

